package com.ardf.service.impl;

import com.ardf.entity.Competition;
import com.ardf.listener.CardPortListener;
import com.ardf.listener.GradePortListener;
import com.ardf.listener.ReplacePortListener;
import com.ardf.service.*;
import com.fazecast.jSerialComm.SerialPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.util.Arrays;
import java.util.List;

import static com.fazecast.jSerialComm.SerialPort.NO_PARITY;
import static com.fazecast.jSerialComm.SerialPort.ONE_STOP_BIT;

@Service
@Slf4j
@Component
public class CardPortServiceImpl implements CardPortService {

    public List<SerialPort> serialPortList;

    @Override
    public List<SerialPort> discoverSerialPort() {

        serialPortList = Arrays.asList(SerialPort.getCommPorts());
        return serialPortList;
    }

    @Override
    public boolean closeSerialPort(SerialPort port) {
        if(port.getPortDescription().equals("CP2102 USB to UART Bridge Controller")){
            log.info("端口:" + port.getSystemPortName() + ",已断开");
            return port.closePort();
        }
        return false;
    }

    public String openSerialPort(CardWebSocketService webSocketService, SerialPort port, WebSocketSession session, ICardService cardService, Integer pageSize, Integer pageNum, Long unitId,Long competitionId) {

        if (port.getPortDescription().equals("CP2102 USB to UART Bridge Controller")) {
            if (port.openPort()) {
                try {
                    port.setBaudRate(9600);//设置串口的波特率
                    port.setNumDataBits(8);//设置每个字所需的数据位数。
                    port.setNumStopBits(ONE_STOP_BIT);//设置每个字所需的停止位数。
                    port.setParity(NO_PARITY);//设置无校验位
                    boolean setComTimes = port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时
                    if (setComTimes) {
                        log.info("串口：" + port.getSystemPortName() + ",已打开");
                        port.addDataListener(new CardPortListener(webSocketService, port.getSystemPortName(), session,cardService,pageSize,pageNum,unitId,competitionId));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return "串口连接失败";
                }
                return "串口连接成功";
            } else {
                return "串口连接失败";
            }

        }
        return "串口连接失败";
    }

    public String openSerialReplacePort(CardWebSocketService webSocketService, SerialPort port, WebSocketSession session, ICardService cardService,IAthleteService athleteService,Long competitionId,Long athleteId) {

        if (port.getPortDescription().equals("CP2102 USB to UART Bridge Controller")) {
            if (port.openPort()) {
                try {

                    port.setBaudRate(9600);//设置串口的波特率
                    port.setNumDataBits(8);//设置每个字所需的数据位数。
                    port.setNumStopBits(ONE_STOP_BIT);//设置每个字所需的停止位数。
                    port.setParity(NO_PARITY);//设置无校验位
                    boolean setComTimes = port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时
                    if (setComTimes) {
                        log.info("串口：" + port.getSystemPortName() + ",已打开");
                        port.addDataListener(new ReplacePortListener(webSocketService, port.getSystemPortName(), session,cardService,athleteService,competitionId,athleteId));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return "串口连接失败";
                }
                return "串口连接成功";
            } else {
                return "串口连接失败";
            }

        }
        return "串口连接失败";
    }

    public String openSerialGradePort(CardWebSocketService webSocketService, SerialPort port, WebSocketSession session, ICardService cardService, IGradeService gradeService, IDetailsService detailsService, IAthleteTeamService teamService, IBatchService batchService, Long projectId, Boolean isRelay, Long competitionId, Competition competition, String project){

        if (port.getPortDescription().equals("CP2102 USB to UART Bridge Controller")) {
            if (port.openPort()) {
                try {

                    port.setBaudRate(9600);//设置串口的波特率
                    port.setNumDataBits(8);//设置每个字所需的数据位数。
                    port.setNumStopBits(ONE_STOP_BIT);//设置每个字所需的停止位数。
                    port.setParity(NO_PARITY);//设置无校验位
                    boolean setComTimes = port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时
                    if (setComTimes) {
                        log.info("串口：" + port.getSystemPortName() + ",已打开");

                        port.addDataListener(new GradePortListener(webSocketService, port.getSystemPortName(), session,cardService,gradeService,detailsService,teamService,batchService,projectId,isRelay,competitionId,competition,project));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return "串口连接失败";
                }
                return "串口连接成功";
            } else {
                return "串口连接失败";
            }

        }
        return "串口连接失败";
    }

}
